/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Model.Attribute;
import Model.Concept;
import Model.Ontology;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author Hicham
 */
public class OntologyPrinterController {

    /**
     * Print the ontology to standard output
     *
     * @param ontology
     */    
    public static void printOntology(Ontology ontology) {
        for (Concept c : ontology.getConcepts()) {
            if (c.isTopConcept()) {
                printConcept(c, 0);
            }
        }

    }

    /**
     * Print the concept to standard output
     * @param c
     * @param level : level of the concept in the tree (starting from 0 for the root) 
     */
    private static void printConcept(Concept c, int level) {
        for (int i = 0; i < level; i++) {
            System.out.print("\t");
        }
        System.out.println(c.getName());
        /* We loop recursively on all the children */
        for (Concept c2 : c.getChildren()) {
            printConcept(c2, level + 1);
        }
    }

    /**
     * this method creates the defaultMutableTreeNode object from the ontology.
     *
     * @param ontology
     * @return DefaultMutableTreeNode
     */
    public static DefaultMutableTreeNode createTreeNodes(Ontology ont, boolean displayAttributes) {
        if (ont != null) {
            if (ont.getName() == null) {
                ont.setName("Thing");
            }

            DefaultMutableTreeNode top = new DefaultMutableTreeNode(ont.getName());
            for (Concept c : ont.getConcepts()) {
                if (c.isTopConcept()) {
                    top.add(createNodes(c, displayAttributes));
                }
            }

            return top;
        } else {
            return null;
        }
    }

    /**
     * Return the tree nodes from a concept.
     *
     * @param concept
     * @return DefaultMutableTreeNode
     */
    public static DefaultMutableTreeNode createTreeNodes(Concept concept, boolean displayAttributes) {
        if (concept != null) {
            return createNodes(concept, displayAttributes);
        }
        return null;
    }

    /**
     * Retrun the tree nodes from a concept using a string query.
     *
     * @param queryConcept
     * @return
     */
    public static DefaultMutableTreeNode createTreeNodes(String queryConcept, boolean displayAttributes) {
        //Concept concept = OntologyController.searchConcept(queryConcept);
        List<Concept> concepts = OntologyController.advancedSearch(queryConcept);
        return createTreeNodes(concepts, displayAttributes);
    }
    
    /** 
     * create the tree from a list of concepts.
     * @param concepts
     * @return DefaultMutableTreeNode
     */
    private static DefaultMutableTreeNode createTreeNodes(List<Concept> concepts, boolean displayAttributes) {
        if (concepts != null && !concepts.isEmpty()) {
            DefaultMutableTreeNode top = new DefaultMutableTreeNode("Search Result...");
            for (Concept c : concepts) {
                top.add(createNodes(c, displayAttributes));
            }
            return top;
        } else {
            return null;
        }
    }

    /**
     * loop recursively on the children of a concept.
     *
     * @param concept
     * @return DefaultMutableTreeNode
     */
    private static DefaultMutableTreeNode createNodes(Concept c, boolean displayAttributes) {
        //DefaultMutableTreeNode conceptDMT = new DefaultMutableTreeNode(c.getName());
        // Let's try with the concept directly and not only its name : 
        DefaultMutableTreeNode conceptDMT = new DefaultMutableTreeNode(c);

        if(displayAttributes) {
            // Add attributes to the tree
            if(!c.getAttributes().isEmpty()) {
                for(Attribute attr : c.getAttributes()) {
                    //conceptDMT.add(new DefaultMutableTreeNode(attr.getName()));
                    conceptDMT.add(new DefaultMutableTreeNode(attr));
                }
            }
        }
        // add the sub concepts to the tree
        if (!c.getChildren().isEmpty()) {
            for (Concept subC : c.getChildren()) {
                conceptDMT.add(createNodes(subC, displayAttributes));
            }
        }

        return conceptDMT;
    }
}
